﻿using JinfuyuanProject.Entity;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;

namespace JinfuyuanProject.Hubs
{
    [HubName("mailboxChatHub")]
    public class MailboxChatHub : Hub
    {
        private static int _unreadCount = 0;
        public void Send(string account, string message)
        {
            if (string.IsNullOrEmpty(account))
            {
                Clients.Caller.showError("未登录用户不能发言");
                return;
            }
            // 给所有人发消息（标记为非本人消息）
            Clients.Others.receiveMessage(account, message, isOwn: false);
            // 给自己发消息（标记为本人消息）
            Clients.Caller.receiveMessage(account, message, isOwn: true);
            // 更新未读计数
            Interlocked.Increment(ref _unreadCount);
            Clients.All.updateUnreadCount(_unreadCount);
        }
        public void MarkAsRead()
        {
            Interlocked.Exchange(ref _unreadCount, 0);
            Clients.All.updateUnreadCount(0);
        }
        public void SendAllMail()
        {
            Service.IMailBoxService _mailBoxService = DependencyResolver.Current.GetService<Service.IMailBoxService>();
            //所有的信箱数据
            List<MessageInfo> list = _mailBoxService.GetData();
            Clients.All.RevMail(Newtonsoft.Json.JsonConvert.SerializeObject(list));
        }
    }
}